package com.zzh.tools.share;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.zzh.tools.Tools;

import java.security.InvalidParameterException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangzhihao
 * @email zhangzhihao@gongjiangren.net
 * @date 2020-08-29
 * @desc 保存数据的操作
 */
public class ToolSharedPreferences {

    private static final Map<String, SharedPreferences> sharedMap = new HashMap<>();

    private static class ToolSharedPreferencesHolder {
        static ToolSharedPreferences toolSharedPreferences = new ToolSharedPreferences();
    }

    private static ToolSharedPreferences getInstance() {
        return ToolSharedPreferencesHolder.toolSharedPreferences;
    }

    private static SharedPreferences getSP(String name) {
        if (TextUtils.isEmpty(name)) {
            throw new NullPointerException("getsp name 不能为空");
        }
        if (sharedMap.containsKey(name)) {
            return sharedMap.get(name);
        }
        // 方案都是私有的
        SharedPreferences sharedPreferences = Tools.getInstance().getContext().getSharedPreferences(name, Context.MODE_PRIVATE);
        sharedMap.put(name, sharedPreferences);
        return sharedPreferences;
    }

    /**
     * 获取string
     *
     * @param key
     * @return
     */
    public static String getString(String key) {
        return getString(Tools.getInstance().getDefaultShareName(), key);
    }

    public static String getString(String shareName, String key) {
        return getString(shareName, key, "");
    }

    public static String getString(String shareName, String key, String defValue) {
        return getSP(shareName).getString(key, defValue);
    }

    /**
     * 设置string
     *
     * @param key
     * @param value
     */
    public static void setString(String key, String value) {
        setString(Tools.getInstance().getDefaultShareName(), key, value);
    }

    public static void setString(String shareName, String key, String value) {
        if (TextUtils.isEmpty(key)) {
            throw new NullPointerException("key 值为null了");
        }
        if (value == null) {
            throw new NullPointerException("value 值为null了");
        }
        getSP(shareName).edit().putString(key, value).commit();
    }

    public static void setString(String... keyAndValue) {
        setString(Tools.getInstance().getDefaultShareName(), keyAndValue);
    }

    public static void setString(String shareName, String... keyAndValue) {
        if (keyAndValue == null) {
            throw new NullPointerException("keyAndValue 值为null了");
        }
        if (keyAndValue.length == 0) {
            return;
        }
        if (keyAndValue.length % 0 != 0) {
            throw new InvalidParameterException("keyAndValue 不符合要求");
        }
        SharedPreferences.Editor editor = getSP(shareName).edit();
        for (int i = 0; i < keyAndValue.length; i += 2) {
            String key = keyAndValue[i];
            String value = keyAndValue[i + 1];
            if (TextUtils.isEmpty(key)) {
                throw new NullPointerException("key 值为null了");
            }
            if (value == null) {
                throw new NullPointerException("value 值为null了");
            }
            editor.putString(key, value);
        }
        editor.commit();
    }


    /**
     * 获取boolean
     *
     * @param key
     * @return
     */
    public static Boolean getBoolean(String key) {
        return getBoolean(Tools.getInstance().getDefaultShareName(), key);
    }

    public static Boolean getBoolean(String shareName, String key) {
        return getBoolean(shareName, key, false);
    }

    public static Boolean getBoolean(String shareName, String key, boolean defValue) {
        return getSP(shareName).getBoolean(key, defValue);
    }

    /**
     * 设置boolean
     *
     * @param key
     * @param value
     */
    public static void putBoolean(String key, boolean value) {
        putBoolean(Tools.getInstance().getDefaultShareName(), key, value);
    }

    public static void putBoolean(String shareName, String key, boolean value) {
        if (TextUtils.isEmpty(key)) {
            throw new NullPointerException("key 值为null了");
        }
        getSP(shareName).edit().putBoolean(key, value).commit();
    }

    /**
     * 获取int
     *
     * @param key
     * @return
     */
    public static int getInt(String key) {
        return getInt(Tools.getInstance().getDefaultShareName(), key);
    }

    public static int getInt(String shareName, String key) {
        return getInt(shareName, key, -1);
    }

    public static int getInt(String shareName, String key, int defValue) {
        return getSP(shareName).getInt(key, defValue);
    }

    /**
     * 设置int
     *
     * @param key
     * @param value
     */
    public static void putInt(String key, int value) {
        putInt(Tools.getInstance().getDefaultShareName(), key, value);
    }

    public static void putInt(String shareName, String key, int value) {
        if (TextUtils.isEmpty(key)) {
            throw new NullPointerException("key 值为null了");
        }
        getSP(shareName).edit().putInt(key, value).commit();
    }

    /**
     * 获取float
     *
     * @param key
     * @return
     */
    public static float getFloat(String key) {
        return getFloat(Tools.getInstance().getDefaultShareName(), key);
    }

    public static float getFloat(String shareName, String key) {
        return getFloat(shareName, key, -1);
    }

    public static float getFloat(String shareName, String key, float defValue) {
        return getSP(shareName).getFloat(key, defValue);
    }

    /**
     * 设置float
     *
     * @param key
     * @param value
     */
    public static void putFloat(String key, float value) {
        putFloat(Tools.getInstance().getDefaultShareName(), key, value);
    }

    public static void putFloat(String shareName, String key, float value) {
        if (TextUtils.isEmpty(key)) {
            throw new NullPointerException("key 值为null了");
        }
        getSP(shareName).edit().putFloat(key, value).commit();
    }

    /**
     * 获取long
     *
     * @param key
     * @return
     */
    public static long getLong(String key) {
        return getLong(Tools.getInstance().getDefaultShareName(), key);
    }

    public static long getLong(String shareName, String key) {
        return getLong(shareName, key, -1);
    }

    public static long getLong(String shareName, String key, long defValue) {
        return getSP(shareName).getLong(key, defValue);
    }

    /**
     * 设置long
     *
     * @param key
     * @param value
     */
    public static void putLong(String key, long value) {
        putLong(Tools.getInstance().getDefaultShareName(), key, value);
    }

    public static void putLong(String shareName, String key, long value) {
        if (TextUtils.isEmpty(key)) {
            throw new NullPointerException("key 值为null了");
        }
        getSP(shareName).edit().putLong(key, value).commit();
    }

}
